class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        if (nums.size() < 3) {
            return 0;
        }
        int n = 0;
        sort(nums.begin(), nums.end());
        int cur = nums.size() - 1;
        int right = cur - 1;
        int left = 0;
        while (cur >= 2) {
            while (left < right) {
                if (nums[left] + nums[right] > nums[cur]) {
                    n = right - left + n;
                    right--;
                } else {
                    left++;
                }
            }
            cur--;
            right = cur - 1;
            left = 0;
        }
        return n;
    }
};